package DB;
import Entities.Powers;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBPowerConnector extends DBConnector {

	static public Powers[] getPowers(int page){
		Powers[] powers=null;
	
		Connection conn=openConnection();
		try {
		PreparedStatement ps= conn.prepareStatement("SELECT name,strength,durability,dexterity,id  FROM character_powers  LIMIT ?,10 ");
		
		ps.setInt(1, page);
		ResultSet rs=ps.executeQuery();
	    	 
		if (rs.next()){
			powers= new Powers[GUI.Manager.resultsPerPage];
			for (int i=0;i<GUI.Manager.resultsPerPage;i++){
	    		 powers[i]=new Powers(rs.getString(1),rs.getDouble("strength"),rs.getDouble("durability"),rs.getDouble("dexterity"),rs.getInt("id"));
	    		 rs.next();
	    	 }
		     ps.close();
		     closeConnection(conn);
			}
		}
	     catch (SQLException e) {
	    	 System.out.println(e.getMessage());
	     }
	     return powers;
	}
	
	static public void setPower(Powers power){
		String sql ="{call set_power(?,?,?,?)}";
		CallableStatement cs1;
		Connection conn=openConnection();
		try {
			cs1 = conn.prepareCall(sql);
			cs1.setString(1,power.getName());
			cs1.setString(2, String.valueOf(power.getDurability()));
			cs1.setString(3,String.valueOf(power.getStrength()));
			cs1.setString(4,String.valueOf(power.getWisdom()));
			cs1.executeUpdate();
	    	cs1.close();
		     closeConnection(conn);
		}
	     catch (SQLException e) {
	    	 System.out.println("sql error occurred " +e.getMessage());
	     }
	}
}